// $Id: popup_image_js.js,v 1.9.2.11 2007/06/18 07:03:57 max Exp $
var current_id = 0;
var imgObj;
var prow;

function changeImg(flag) {
	if (!imgObj)
		imgObj = document.getElementById('img');
	if (!imgObj)
		return false;

	if (flag < 0) {
		return false;
	} else if (flag > images.length-1) {
		if (current_id == flag)
			return false;
		flag = images.length-1;
	}

	current_id = flag;
	if (!images[current_id])
		return false;

	if (!images[current_id][3]) {
		images[current_id][0].src = images[current_id][2];
		images[current_id][3] = true;
	}
	if (current_id+1 <= images.length-1 && !images[current_id+1][3]) {
		images[current_id+1][0].src = images[current_id+1][2];
		images[current_id+1][3] = true;
	}
	if (current_id-1 >= 0 && !images[current_id-1][3]) {
		images[current_id-1][0].src = images[current_id-1][2];
		images[current_id-1][3] = true;
	}

	if (!prow)
		prow = document.getElementById('prow');
	if (!prow)
		return false;

/*
	Create pages row
*/
	prow.innerHTML = '';
	var sPage = 0;
	var ePage = images.length;
	var rpagesCount = 0;
	var rpagesCurrent = 0;
	if (max_nav_pages > 0) {
		rpagesCount = Math.ceil((images.length)/max_nav_pages);
		rpagesCurrent = Math.ceil((current_id+1)/max_nav_pages);
	}
	if (rpagesCount > 1) {
		sPage = (rpagesCurrent-1)*max_nav_pages;
		ePage = (rpagesCurrent == rpagesCount) ? images.length : sPage+max_nav_pages;
		if (rpagesCurrent == 1) {
			pre_reload(larrow2_grey.src, 'larr2', false);
		} else {
			pre_reload(larrow2.src, 'larr2', false);
		}
		
		if (rpagesCurrent == rpagesCount) {
			pre_reload(rarrow2_grey.src, 'rarr2', false);
		} else {
			pre_reload(rarrow2_grey.src, 'rarr2', false);
		}
	}

	if (current_id == 0) {
		pre_reload(larrow_grey.src, 'larr', false);
	} else {
		pre_reload(larrow.src, 'larr', false);
	}
	if (current_id == images.length-1) {
		pre_reload(rarrow_grey.src, 'rarr', false);
	} else {
		pre_reload(rarrow.src, 'rarr', false);
	}

	var tbl = prow.appendChild(document.createElement("TABLE"));
	var r = tbl.insertRow(-1);
	var add = 0;
	for (var i = sPage; i < ePage; i++) {
		var t = r.insertCell(-1);
		if (current_id == i) {
			if ((localBrowser == "MSIE") && (isHttps))  {
				t.style.verticalAlign = 'top';
				t.innerHTML = i+1;
				t.className = 'NavigationCellSel';
				t.style.width = "auto";
			} else {
				t.className = 'NavigationCellSel';
				t.innerHTML = i+1;
			}
		} else {
			t.className = ((i+1) >= 100) ? 'NavigationCellWide' : 'NavigationCell';
			if ((localBrowser == "MSIE") && (isHttps))  {
				var back_image_url = t.currentStyle.backgroundImage;
				var result = back_image_url.match(/url\(\"([^\"]+)/);
				var img_src = result[1];
				t.className = "";
				t.style.textAlign = "center";
				var x = t.appendChild(document.createElement("IMG"));
				x.id = "i_"+(i+1);
				pre_reload(img_src, x.id, false);
				var d = t.appendChild(document.createElement("DIV"));
				d.style.position = "absolute";
				var d_left = d.offsetLeft;
				var width = 0;
				var count = 8;
				if ((i+1) < 10) {
					width = 25;
				} else if ((i+1) > 9 && (i) < 100){
					width = 27;
				} else {
					width = 27;
					add+=3;
				}
				if (i < current_id) {
					d.style.left = d_left - (width + i * count);
				} else {
					d.style.left = d_left - (width + (i - 1) * count) + add;
				}
				var a = d.appendChild(document.createElement("A"));
				a.title = lbl_page+" #"+(i+1);
				a.href = "javascript: void(0);";
				a.onclick = new Function("", "changeImg("+i+");");
				a.innerHTML = i+1;
			} else {
				var x = t.appendChild(document.createElement("A"));
				x.title = lbl_page+" #"+(i+1);
				x.href = "javascript: void(0);";
				x.onclick = new Function("", "changeImg("+i+");");
				x.innerHTML = i+1;
				var x = t.appendChild(document.createElement("IMG"));
				x.src = spc.src;
			}
		}
	}

	imgObj.onload = imgOnLoad;
	imgObj.alt = '';
	pre_reload(images[current_id][0].src, 'img', true);
}

function reloadImage(src, imgName, is_add) {
	var now = new Date();
	var img = document.getElementById(imgName);
	if (img) {
		if (is_add) {
			img.src = src +"?"+ now.getTime();
		} else {
			img.src = src;
		}
	}
}

function pre_reload(src, imgName, is_add) {
	setTimeout("reloadImage('"+src+"', '"+imgName+"', "+is_add+")", 5);
}

function delay(flag) {
	if (!flag) {
		timer = setTimeout('delay(true)', 100);
	} else {
		return;
	}
}


function imgOnLoad(obj) {
	if (images.length == 0 && obj)
		imgObj = obj;

	window.resizeTo(imgObj.width, imgObj.height);

	expandWindowX(false, 10, screen.width);
	expandWindowY(false, 10, screen.height);

	if (images.length == 0)
		return true;

	imgObj.alt = images[current_id][1];

	return true;
}
